package test.model;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Action;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.TableColumn;

import test.model.table.ButtonRenderer;
import test.model.table.ImageRenderer;

public class ContactFrame extends JFrame implements PropertyChangeListener{
	
	JTable _table;
	JScrollPane _jscrollPn;
	JPanel pnButton;
	JButton btnSearch, btnAdd;
	JTextField txtSearch;
	
	public ContactFrame(){
		super("Contact list");
		initComponent();
		
		//This is should be put into Application
		File dataF = new File(ContactDAO.getPath() + "data");
		if(!dataF.exists())
			dataF.mkdirs();
		
		dataF = new File(ContactDAO.getPath() + "images");
		if(!dataF.exists())
			dataF.mkdirs();
		
		loadData();
	}
	
	private void initComponent(){
		setBounds(20, 10, 400, 650);
		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		
		//JTable
		ContactTableModel model = new ContactTableModel();
		_table = new JTable(model);
		_table.addMouseListener(new MouseAdapter() {
			   public void mouseClicked(MouseEvent e) {
			      if (e.getClickCount() == 2) {
			         JTable target = (JTable)e.getSource();
			         int row = target.getSelectedRow();
			         int column = target.getSelectedColumn();
			         // do some action
			         
			         UpdateContactFrame ucf = new UpdateContactFrame(((ContactTableModel)(_table.getModel())).getItemAt(row));
			         ucf.addListener(ContactFrame.this);
			         ucf.setVisible(true);
			      }
			   }
			});
		
		_table.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer());
		
		Action actionDel = new AbstractAction(){

			@Override
			public void actionPerformed(ActionEvent ev) {
				// TODO Auto-generated method stub
				int row = Integer.parseInt(ev.getActionCommand());
				((ContactTableModel)(_table.getModel())).removeItem(row);
			}
			
		};
		//_table.getColumnModel().getColumn(3).setCellRenderer(new ButtonRenderer());
		_table.getColumnModel().getColumn(3).setCellRenderer(new ButtonColumn(_table, actionDel, 3));
		
		_table.setRowHeight(100);
		
		_jscrollPn = new JScrollPane(_table);
		c.add(_jscrollPn, BorderLayout.CENTER);
		
		pnButton = new JPanel(new GridBagLayout());
		
		txtSearch = new JTextField(10);
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = gbc.LINE_START;
		gbc.fill = gbc.HORIZONTAL;
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.weightx = 0.7;
		pnButton.add(txtSearch, gbc);
		
		btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String kw = txtSearch.getText();
				((ContactTableModel)(_table.getModel())).filter(kw);
			}
		});

		gbc = new GridBagConstraints();
		pnButton.add(btnSearch, gbc);
		
		btnAdd = new JButton("New");
		btnAdd.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				UpdateContactFrame ucf = new UpdateContactFrame();
				ucf.addListener(ContactFrame.this);
				ucf.setVisible(true);
			}
			
		});
		gbc = new GridBagConstraints();
		gbc.anchor = gbc.LINE_START;
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.weightx = 0.7;
		pnButton.add(btnAdd, gbc);
		
		c.add(pnButton, BorderLayout.NORTH);
		
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we){
				System.exit(0);
			}
		});
	}
	
	private void loadData(){
		ContactDAO dao = new ContactDAO();
		try {
			ArrayList<ContactItem> list = dao.loadAllItems();
			((ContactTableModel)(_table.getModel())).setContacts(list);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void propertyChange(PropertyChangeEvent e) {
		// TODO Auto-generated method stub
		ContactItem item = (ContactItem)(e.getNewValue());
		
		if(e.getPropertyName().equals("NewItem"))
			((ContactTableModel)(_table.getModel())).addItem(item);
		
		((ContactTableModel)(_table.getModel())).fireTableDataChanged();
		System.out.println("Get new item");
	}
}
